function callback(entries: any) {
  for (let i of entries) {
    if (i.isIntersecting) {
      let img = i.target
      let trueSrc = img.getAttribute('data-src')
      img.setAttribute('src', trueSrc)
      observer?.unobserve(img)
    }
  }
}
let observer: IntersectionObserver | null = null
try {
  observer = new IntersectionObserver(callback)
} catch (error) {
  observer = null
}

export const LazyImg = {
  beforeMount(el: Element, binding: { value: boolean }) {
    if (!observer || binding.value === false) return
    let trueSrc = el.getAttribute('src')
    el.setAttribute('src', '' as string)
    el.setAttribute('data-src', trueSrc as string)
    observer.observe(el)
  },
  beforeUnmount(el: Element) {
    observer?.unobserve(el)
  }
}
